package defaulting

import (
	"phant-operator/pkg/apis/phant/v1alpha1"

	corev1 "k8s.io/api/core/v1"
)

const (
	defaultPatroniImage = "patroni"

	defaultPostgresImage = "postgres"
)

func SetPostgresClusterDefault(pg *v1alpha1.PostgresCluster) {
	SetPostgresSpecDefault(pg)
	if pg.Spec.Postgres != nil {
		SetPostgresSpecDefault(pg)
	}
	if pg.Spec.Patroni != nil {
		SetPatroniSpecDefault(pg)
	}
}

func SetPostgresSpecDefault(pg *v1alpha1.PostgresCluster) {
	if string(pg.Spec.ImagePullPolicy) == "" {
		pg.Spec.ImagePullPolicy = corev1.PullIfNotPresent
	}
	if len(pg.Spec.Version) >= 0 || pg.Spec.Postgres.Version != nil {
		if pg.Spec.Postgres.BaseImage == "" {
			pg.Spec.Postgres.BaseImage = defaultPostgresImage
			if pg.Spec.Patroni != nil {
				pg.Spec.Postgres.BaseImage = defaultPatroniImage
			}
		}
	}
	if pg.Spec.ServiceAccountName != "" && pg.Spec.Postgres.ServiceAccountName == "" {
		pg.Spec.Postgres.ServiceAccountName = pg.Spec.ServiceAccountName
	}
}

func SetPatroniSpecDefault(pg *v1alpha1.PostgresCluster) {
	if string(pg.Spec.Patroni.DCSType) == "" {
		pg.Spec.Patroni.DCSType = v1alpha1.KubernetesDCSType
	}
}
